package com.wedding.controller;

import com.wedding.pojo.User;
import com.wedding.service.ItemsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageInfo;

@Controller
@RequestMapping("/item")
public class ItemsController {

	private  final Logger logger = LoggerFactory.getLogger(ItemsController.class); 
	@Autowired
	private ItemsService itemsService;
	
	@RequestMapping(value="/itemList")
	public ModelAndView itemList(@RequestParam(defaultValue="1", required=true) Integer pageNum,
			@RequestParam(defaultValue="10", required=true) Integer pageSize) {
		Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		if(principal != null && principal instanceof UserDetails) {
			User user = (User) principal;
			logger.debug(user+"===============");
		}
		System.err.println("===========");
		pageNum = pageNum<1?1:pageNum;
		pageSize = pageSize<1?1:pageSize;
		// service层使用到了Mybatis分页插件PageHelper
		PageInfo pageInfo = itemsService.itemList(pageNum, pageSize);
		ModelAndView mv=new ModelAndView("itemList");
		mv.addObject("pageInfo", pageInfo);
		mv.addObject("rows", pageInfo.getList());
		return mv;
	}
	@RequestMapping(value="/itemList2", method = RequestMethod.POST)
	public ModelAndView itemList2() {
		ModelAndView mv=new ModelAndView("itemList2");
		return mv;
	}
	@RequestMapping(value="/itemList3", method = RequestMethod.POST)
	public ModelAndView itemList3() {
		ModelAndView mv=new ModelAndView("itemList2");
		return mv;
	}
}
